#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int M = 2e6 + 10;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
typedef pair<ll, ll> PII;
ll n, m,a,b,c;
ll h[N], w[N], e[M], ne[M], idx;
ll dist[N];
bool st[N];
void add(ll a, ll b, ll c) {e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;}
void dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({ 0, 1 });

    while (heap.size())
    {
        auto t = heap.top();
        heap.pop();

        ll ver = t.second, distance = t.first;

        if (st[ver]) continue;
        st[ver] = true;

        for (ll i = h[ver]; i != -1; i = ne[i])
        {
            ll j = e[i];
            if (dist[j] > dist[ver] + w[i])
            {
                dist[j] = dist[ver] + w[i];
                heap.push({ dist[j], j });
            }
        }
    }
}
int main()
{
    close()
    cin >> n >> m;

    memset(h, -1, sizeof h);
    memset(st, 0, sizeof st);
    
    while (m--)
    {
        cin >> a >> b >> c; //a到b 边长为c
        add(a, b, c);
    }

    dijkstra();
    for(int i = 1; i <= n; i++)
    {
        if(dist[i]>=0x3f3f3f3f3f3f3f3f) cout<<-1<<" ";
        else cout<<dist[i]<<" ";
    }
    cout << '\n';
    return 0;
}

//有向图,权值为正